<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>页面滚动案例</title>
    <style>
        .head {
            height: 200px;
            width: 500px;
            background-color: aqua;
            margin-bottom: 10px;
        }
        
        .nav {
            width: 500px;
            height: 300px;
            background-color: brown;
            margin-bottom: 10px;
        }
        
        .bar {
            width: 500px;
            height: 400px;
            background-color: blue;
            margin-bottom: 10px;
        }
        
        .main {
            width: 500px;
            height: 800px;
            background-color: pink;
        }
        
        .bulk {
            width: 980px;
            margin: auto;
            display: flex;
            flex-direction: column;
            align-items: center;
            position: relative;
        }
        
        .fl {
            /* position: relative; */
            display: inline-block;
        }
        
        .fix {
            width: 50px;
            height: 100px;
            background-color: red;
            position: absolute;
            right: 186px;
            top: 520px;
        }
    </style>
</head>

<body>
    <div class="bulk">
        <div class="head fl">
            这是头文件
        </div>
        <div class="fix fl">
            <span style="display:none;">我是定位</span>
        </div>
        <div class="nav fl">
            这是导航
        </div>
        <div class="bar fl">
            这是柜台
        </div>
        <div class="main fl">
            这是主体
        </div>

    </div>
    <script>
        var fix = document.querySelector('.fix');
        var span = document.querySelector('span');
        var head = document.querySelector('.head');
        var nav = document.querySelector('.nav');
        document.addEventListener('scroll', function() {
            if (window.pageYOffset >= nav.offsetTop) {
                fix.style.position = 'fixed';
                fix.style.top = +'px';
                fix.style.right = 454 + 'px';
                span.style.display = 'block';

            } else {
                fix.style.position = 'absolute';
                fix.style.top = 520 + 'px';
                fix.style.right = 186 + 'px';

            }
        })
    </script>
</body>

</html>